﻿using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Production_Modules;
using MES.Server.API.Dto.Warehouse;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Production_Modules
{
    /// <summary>
    /// 仓库查询命令
    /// </summary>
    public class WarehouseQueryCommandHandler : IRequestHandler<WarehouseQueryCommand, ApiResult<ApiPaging<WarehouseModel>>>
    {
        /// <summary>
        /// 仓库仓储
        /// </summary>
        IRepository<WarehouseModel> wardRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="wardRep"></param>
        public WarehouseQueryCommandHandler(IRepository<WarehouseModel> wardRep)
        {
            this.wardRep = wardRep;
        }
        
        public Task<ApiResult<ApiPaging<WarehouseModel>>> Handle(WarehouseQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<WarehouseModel>> result=new ApiResult<ApiPaging<WarehouseModel>>();
            result.Code=StatusCode.Success;
            result.Message=StatusCode.Success.ToString();
            var list = wardRep.GetAll().Where(x=>!x.IsDel);
            int totalcount = list.Count();
            int totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.Size);
            list = list.OrderByDescending(x => x.Id).Skip((request.Index-1) * request.Size).Take(request.Size);
            ApiPaging<WarehouseModel> ping = new ApiPaging<WarehouseModel>();
            ping.Totalcount = totalcount;
            ping.TotalPage = list.ToList();
            result.Data = ping;
            return Task.FromResult(result);
            

        }
    }
}
